package com.niit.yhserver.controller;

import com.niit.yhpojo.dto.AdminDTO;
import com.niit.yhpojo.entity.Admin;
import com.niit.yhserver.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin")
@Slf4j
@CrossOrigin("*")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 显示全部管理员
     * @return
     */
    @GetMapping("/getAll")
    public List<Admin> getAllAdmin(){
        return adminService.getAllAdmin();
    }

    /**
     * 新增管理员
     * @param adminDTO
     * @return
     */
    @PostMapping("/addAdmin")
    public String addAdmin(@RequestBody AdminDTO adminDTO){
        log.info("新增员工：{}",adminDTO);
        adminService.addAdmin(adminDTO);
        return "新增管理员成功！";
    }

    /**
     * 删除员工
     * @param id
     * @return
     */
    @DeleteMapping("/del/{id}")
    public String deleteAdmin(@PathVariable("id") Integer id){
        adminService.deleteAdminById(id);
        return "删除管理员成功！";
    }

    /**
     * 更改管理员信息
     * @param adminDTO
     * @return
     */
    @PutMapping("/updateAdmin")
    public String updateAdmin(@RequestBody AdminDTO adminDTO){
        log.info("编辑管理员信息:{}",adminDTO);
        adminService.updateAdmin(adminDTO);
        return "编辑管理员信息成功！";
    }

    @GetMapping("/getOne/{id}")
    public Admin getOneAdmin(@PathVariable Integer id){
        return adminService.findAdminOneById(id);
    }

}
